<template>
	<a-card :bordered="false">
		<a-form border="false" ref="searchFormRef" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:scroll="{ x: 'max-content' }"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen"> 新增</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'privacyPolicy'">
					<a-tooltip :title="record.privacyPolicy" v-if="record.privacyPolicy && record.privacyPolicy.length > 10">
						<span class="ellipsis-text">{{ record.privacyPolicy.slice(0, 10) }}...</span>
					</a-tooltip>
					<span v-else class="ellipsis-text">{{ record.privacyPolicy }}</span>
				</template>
				<template v-else-if="column.dataIndex === 'legalNotice'">
					<a-tooltip :title="record.legalNotice" v-if="record.legalNotice && record.legalNotice.length > 10">
						<span class="ellipsis-text">{{ record.legalNotice.slice(0, 10) }}...</span>
					</a-tooltip>
					<span v-else class="ellipsis-text">{{ record.legalNotice }}</span>
				</template>
				<template v-else-if="column.dataIndex === 'helpGuide'">
					<a-tooltip :title="record.helpGuide" v-if="record.helpGuide && record.helpGuide.length > 10">
						<span class="ellipsis-text">{{ record.helpGuide.slice(0, 10) }}...</span>
					</a-tooltip>
					<span v-else class="ellipsis-text">{{ record.helpGuide }}</span>
				</template>
				<template v-else-if="column.dataIndex === 'aboutText'">
					<a-tooltip :title="record.aboutText" v-if="record.aboutText && record.aboutText.length > 10">
						<span class="ellipsis-text">{{ record.aboutText.slice(0, 10) }}...</span>
					</a-tooltip>
					<span v-else class="ellipsis-text">{{ record.aboutText }}</span>
				</template>
				<template v-else-if="column.dataIndex === 'qrCodeUrl'">
					<template v-if="record.qrCodeUrl">
						<img :src="record.qrCodeUrl" alt="二维码" style="max-width: 50px; max-height: 50px" />
					</template>
					<template v-else> -</template>
				</template>
				<template v-else-if="column.dataIndex === 'officialSealUrl'">
					<template v-if="record.officialSealUrl">
						<img :src="record.officialSealUrl" alt="公章" style="max-width: 50px; max-height: 50px" />
					</template>
					<template v-else> -</template>
				</template>
				<template v-else-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" primary size="small" @click="formRef.onOpen(record)">编辑</a-button>
						<a-divider type="vertical" />
						<a-popconfirm title="确定要删除吗？" @confirm="del(record.id)">
							<a-button type="link" danger size="small">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
				<template v-else>
					<span>{{ record[column.dataIndex] || '-' }}</span>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="accessLog">
	import configApi from '@/api/website/config/configApi' // 假设的 API 模块
	import Form from '@/views/website/config/form.vue'
	import { ref } from 'vue'

	const searchFormRef = ref()
	const searchFormState = ref({})
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '公司名称',
			dataIndex: 'companyName',
			align: 'center'
		},
		{
			title: '公司编码',
			dataIndex: 'companyCode',
			align: 'center'
		},
		{
			title: '公司英文',
			dataIndex: 'companyEnglish',
			align: 'center'
		},
		{
			title: '城市名称',
			dataIndex: 'city',
			align: 'center'
		},
		{
			title: '已诉调对接案件数量',
			dataIndex: 'referredMediationCount',
			align: 'center'
		},
		{
			title: '正在调解案件数量',
			dataIndex: 'activeMediationCount',
			align: 'center'
		},
		{
			title: '已正式立案案件数量',
			dataIndex: 'filedCaseCount',
			align: 'center'
		},
		{
			title: '已判决案件数量',
			dataIndex: 'adjudicatedCaseCount',
			align: 'center'
		},
		{
			title: '已执行案件数量',
			dataIndex: 'enforcedCaseCount',
			align: 'center'
		},
		{
			title: '隐私声明',
			dataIndex: 'privacyPolicy',
			align: 'center',
			width: 200,
			ellipsis: true
		},
		{
			title: '法律声明',
			dataIndex: 'legalNotice',
			align: 'center',
			width: 200,
			ellipsis: true
		},
		{
			title: '使用帮助',
			dataIndex: 'helpGuide',
			align: 'center',
			width: 200,
			ellipsis: true
		},
		{
			title: '关于我们',
			dataIndex: 'aboutText',
			align: 'center',
			width: 200,
			ellipsis: true
		},
		{
			title: '备案号',
			dataIndex: 'registration',
			align: 'center'
		},
		{
			title: '地址',
			dataIndex: 'address',
			align: 'center'
		},
		{
			title: '电话',
			dataIndex: 'phone',
			align: 'center'
		},
		{
			title: '二维码',
			dataIndex: 'qrCodeUrl',
			align: 'center'
		},
		{
			title: '域名',
			dataIndex: 'websiteUrl',
			align: 'center'
		},
		{
			title: '公章图片地址',
			dataIndex: 'officialSealUrl',
			align: 'center'
		},
		{
			title: '微信公众号名称',
			dataIndex: 'weChatOfficialAccount',
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			fixed: 'right'
		}
	]
	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		return configApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}

	const del = (id) => {
		configApi.del(id).then((res) => {
			tableRef.value.refresh()
		})
	}
</script>

<style scoped>
	.ellipsis-text {
		display: inline-block;
		width: 100%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		max-width: 200px;
	}
</style>
